<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\User;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\InfoBox;
use Encore\Admin\Layout\Column;

class TodayUserController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('用户管理')
            ->description('今日新增')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑')
            ->description('编辑用户')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('创建用户')
            ->description('创建用户')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User);

        $grid->model()->whereDate('created_at', date('Y-m-d'));
        $grid->id('Id');
        $grid->phone('手机号');
        $grid->email('邮箱');
        // $grid->password('密码');
        // $grid->security('交易密码');
        $grid->nickname('昵称');
        $grid->avatar('头像')->lightbox(['width' => 50, 'height' => 50]);        
        $grid->column('status','用户状态')->editable('select', [1 => '正常', 2 => '冻结'])->sortable();
        $grid->code('邀请码');
        $grid->created_at('创建时间');
        $grid->updated_at('修改时间');
        $grid->actions(function ($actions) {
            // $actions->disableDelete();
            // $actions->disableEdit();
            $actions->disableView();
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->id('Id');
        $show->phone('Phone');
        $show->email('Email');
        $show->password('Password');
        $show->security('Security');
        $show->nickname('Nickname');
        $show->avatar('Avatar');
        $show->token('Token');
        $show->pid('Pid');
        $show->status('Status');
        $show->code('Code');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User);

        $form->hidden('status')->default(1);//用户状态默认正常
        $form->mobile('phone', '手机号');
        $form->email('email', '邮箱');
        $form->password('password', '密码');
        $form->text('security', '交易密码');
        $form->text('nickname', '昵称');
        $form->image('avatar', '头像')->move('public/upload/avatar/')->uniqueName();
        $form->text('code', '邀请码');
        $form->saving(function (Form $form) {
            $form->password = bcrypt($form->password);
            $form->security = bcrypt($form->security);
        });
        $form->saved(function (Form $form){
            //@todo增加算力
        });

        return $form;
    }
}
